//
//  NSArray+utils.m
//  helper
//
//  Created by huangyi on 12-10-29.
//  Copyright (c) 2012年 huangyi. All rights reserved.
//

#import "NSArray+utils.h"
#import "NSDictionary+utils.h"
#import "NSData+utils.h"

@implementation NSArray (utils)

-(id)firstObject{
    if (self.count) {
        return [self objectAtIndex:0];
    }else{
        return nil;
    }
}

-(NSArray*)sortAscending{
    return [self sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
}

-(NSArray*)reverseObjects{
    NSEnumerator *link=[self reverseObjectEnumerator];
    return [link allObjects];
}

-(NSData*)jsonData{
    NSData *data=nil;
    NSError *error=nil;
    data=[NSJSONSerialization dataWithJSONObject:self options:0 error:&error];
    if (error) {
        NSLog(@"%@",error.description);
    }
    return data;
}
-(NSString*)jsonString{
    NSData* data=[self jsonData];
    return data.stringValue;
}

-(BOOL)containsString:(NSString*)string{
    for (id val in self) {
        if ([val isKindOfClass:[NSString class]]) {
            NSString *str=val;
            if ([str isEqualToString:string]) {
                return YES;
            }
        }
    }
    return NO;
}

+(NSArray*)getArgs:(id)arguments,...NS_REQUIRES_NIL_TERMINATION{
    NSArray *result=nil;
    if(arguments!=nil){
        NSMutableArray *args=[NSMutableArray array];
        va_list vl;
        id argv=arguments;
        va_start(vl,arguments);
        while(argv!=nil){
            [args addObject:argv];
            argv=(__bridge id)(va_arg(vl, void*));
        }
        va_end(vl);
        for(id arg in args){
            NSLog(@"%@",[arg class]);
        }
        result=[NSArray arrayWithArray:args];
    }
    return result;
}

@end

